package com.droid.lib.controls.demo.view;

import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;

import com.avast.android.dialogs.fragment.SimpleDialogFragment;
import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.widget.HorizontalListView;
import com.droid.lib.controls.widget.RadarItemAdapter;
import com.droid.lib.controls.widget.XRadarView;

import java.util.List;

import static com.droid.lib.controls.widget.XRadarView.*;

/**
 * 雷达表
 */
public class RedarFragment extends Fragment {
    private XRadarView mrvDistributed;
    private HorizontalListView mhvDistributed;
    private RadarItemAdapter mItemAdapter;
    CharSequence titles[] = new CharSequence[]{"郑州", "开封", "洛阳", "平顶山", "安阳", "鹤壁", "新乡", "焦作"};
    // 每种属性的值（0~1.0）
    double percents[] = new double[]{0.8, 0.8, 0.9, 1, 0.6, 0.5, 0.77, 0.9};
    double percents1[] = new double[]{0.2, 0.2, 0.3, 0.8, 0.8, 0.6, 0.1, 0.6};
    double percents2[] = new double[]{0.4, 0.4, 0.5, 0.9, 1, 0.4, 0.9, 0.5};
    double percents3[] = new double[]{0.6, 0.6, 0.7, 0.6, 0.2, 0.2, 0.5, 0.4};
    public RedarFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_redar, null);
        mrvDistributed = (XRadarView) view.findViewById(R.id.rv_distributed);
        mhvDistributed = (HorizontalListView) view.findViewById(R.id.hv_distributed);
        mItemAdapter = new RadarItemAdapter(getContext());
        mhvDistributed.setAdapter(mItemAdapter);
        // 设置各区域颜色的数组
        // 如果设置了colors则会每个区域显示不同的颜色，否则所有区域显示同一个颜色   操作1
        mrvDistributed.setColors(null);
        // 如果想设置所有区域同一种颜色，可以设置dataColor                    操作2    （操作1和操作2 互斥）
//        radarView.setDataColor(Color.parseColor("#999900"));

        // 设置各项显示的值的文本
//        radarView.setValues(values);
        // 设置各项的图标
//        radarView.setDrawables(drawables);
        // 设置允许各个点之间连线
        mrvDistributed.setEnabledBorder(true);
        mrvDistributed.setTitleColor(Color.WHITE);
        mrvDistributed.setLineColor(Color.WHITE);
        // 显示圆点
        mrvDistributed.setEnabledShowPoint(true);
        // 绘制正n变形
        mrvDistributed.setEnabledPolygon(true);
        // 绘制渐变环
        mrvDistributed.setEnabledShade(true);
        // 绘制半径
        mrvDistributed.setEnabledRadius(true);
        // 绘制标题，数值和图标等
        mrvDistributed.setEnabledText(true);
        // 开启动画
        mrvDistributed.setEnabledAnimation(true);
        // 设置层数
        mrvDistributed.setLayerCount(5);
        // 无渐变色的单一色
        mrvDistributed.setRedarItemCount(4);
        mrvDistributed.setBorderColor(0, Color.RED);
        mrvDistributed.setSingleColor(0, Color.RED);
        mrvDistributed.setPointColor(0, ContextCompat.getColor(getContext(), R.color.colorAccent));
        mrvDistributed.setPercents(0, percents);
        mrvDistributed.setName(0, "工商银行");
        mrvDistributed.setSingleColor(1, Color.YELLOW);
        mrvDistributed.setPointColor(1, Color.YELLOW);
        mrvDistributed.setBorderColor(1, Color.GREEN);
        mrvDistributed.setPercents(1, percents1);
        mrvDistributed.setName(1, "农业银行");
        mrvDistributed.setSingleColor(2, Color.BLUE);
        mrvDistributed.setPointColor(2, Color.BLUE);
        mrvDistributed.setBorderColor(2, Color.CYAN);
        mrvDistributed.setPercents(2, percents2);
        mrvDistributed.setName(2, "建设银行");
        mrvDistributed.setSingleColor(3, Color.MAGENTA);
        mrvDistributed.setPointColor(3, Color.MAGENTA);
        mrvDistributed.setBorderColor(3, Color.LTGRAY);
        mrvDistributed.setPercents(3, percents3);
        mrvDistributed.setName(3, "交通银行");
        mItemAdapter.setData(mrvDistributed.getRedarItems());
        mrvDistributed.setEnabledRegionShader(false);
        // 配置区域渐变, 第二个参数需要微调才能获得好的结果
        mrvDistributed.setRegionShaderConfig(new int[]{Color.YELLOW, Color.RED}, new float[]{0.2f, 0.6f});
        // 雷达图各项值比例

        // 设置各项标题
        mrvDistributed.setTitles(titles);
        mhvDistributed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mrvDistributed.setShow(position, !mrvDistributed.isShow(position));
                mItemAdapter.notifyDataSetChanged();
            }
        });
        // 雷达图标题和图标的点击事件
        mrvDistributed.setOnTitleClickListener(new OnTitleClickListener() {
            @Override
            public void onTitleClick(XRadarView view, int position, int x, int y, Rect rect) {
//                SimpleDialogFragment.createBuilder(getContext(), getActivity().getSupportFragmentManager()).setTitle("区域分布")
//                        .setMessage(titles[position]).setNegativeButtonText("确定").show();
            }

            @Override

            public void onCircleClick(XRadarView view, int parent, int position, int touchX, int touchY, Rect titleRect) {
//                SimpleDialogFragment.createBuilder(getContext(), getActivity().getSupportFragmentManager()).setTitle("区域分布")
//                        .setMessage(parent + "" + titles[position]).setNegativeButtonText("确定").show();
            }
        });
        mrvDistributed.setOnCirclePop(new OnCirclePop() {
            @Override
            public View onTitleClick(int position, int child, int touchX, int touchY) {
                return null;
            }

            @Override
            public View onChildClick(int position, int child, int touchX, int touchY) {
                return null;
            }

        });
        mrvDistributed.loadAnimation(true);
        return view;
    }
}
